package com.yunduansing.core.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.yunduansing.core.commons.DbCommon;
import com.yunduansing.core.model.TemplateTypeModel;
import com.yunduansing.core.service.TemplateTypeService;
import com.yunduansing.core.utils.CommonUtil;
import com.yunduansing.core.utils.ResultUtil;

@Controller
@RequestMapping("/templatetype")
public class TemplateTypeController {
	@Autowired
	private TemplateTypeService service;
	
	@RequestMapping(value="/index",method=RequestMethod.GET)
	public ModelAndView index(HttpServletRequest req){
		ModelAndView view=new ModelAndView();
		view.setViewName("/beetl/templatetype/index");
		System.out.println(req.getHeader("x-forwarded-for"));
		return view;
	}
	
	@ResponseBody
	@RequestMapping(value="/page",method=RequestMethod.POST,produces="application/json;charset=utf-8")
	public String pagination(DbCommon.Grid grid,String key){
		return CommonUtil.toJsonWithGson(service.pagination(grid, key));
	}
	
	@RequestMapping(value="/detail",method=RequestMethod.GET)
	public ModelAndView detail(Model model,@RequestParam(value="id",required=false) Integer id){
		ModelAndView view=new ModelAndView();
		view.setViewName("/beetl/templatetype/detail");
		TemplateTypeModel type;
		if(id==null){
			type=new TemplateTypeModel();
		}else{
			type=service.getById(id);
		}
		model.addAttribute("model", type);
		return view;
	}
	
	@ResponseBody
	@RequestMapping(value="/delete",method=RequestMethod.POST,produces="application/json;charset=utf-8")
	public String delete(@RequestParam("ids[]") List<Integer> ids){
		return CommonUtil.toJsonWithGson(service.deleteByIds(ids));
	}
	
	@ResponseBody
	@RequestMapping(value="/save",method=RequestMethod.POST,produces="application/json;charset=utf-8")
	public String save(TemplateTypeModel model){
		return CommonUtil.toJsonWithGson(service.save(model));
	}
}
